﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web.Security;

namespace KingSite.Library.Security {
    public class AES {

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="encrypt">待加密字符串</param>
        /// <param name="key">密钥</param>
        /// <returns>加密失败返回 null</returns>
        public static string Encrypt(string encrypt, string key) {
            return Encrypt(encrypt, key, key.Length);
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="decrypt">待解密字符串</param>
        /// <param name="key">密钥</param>
        /// <returns>解密失败返回 null</returns>
        public static string Decrypt(string decrypt, string key) {
            return Decrypt(decrypt, key, key.Length);
        }

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="encrypt">待加密字符串</param>
        /// <param name="key">密钥</param>
        /// <param name="keySize">密钥位数（128位，256位）</param>
        /// <returns>加密失败返回 null</returns>
        public static string Encrypt(string encrypt, string key, int keySize) {
            try {
                byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(key);
                byte[] strEncryptArray = UTF8Encoding.UTF8.GetBytes(encrypt);
                byte[] resultArray = null;
                using (RijndaelManaged rDel = new RijndaelManaged()) {
                    rDel.KeySize = keySize;
                    rDel.Key = keyArray;
                    rDel.Mode = CipherMode.ECB;
                    rDel.Padding = PaddingMode.Zeros;
                    ICryptoTransform cTransform = rDel.CreateEncryptor();
                    resultArray = cTransform.TransformFinalBlock(strEncryptArray, 0, strEncryptArray.Length);
                }
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            catch {
                return null;
            }            
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="decrypt">待解密字符串</param>
        /// <param name="key">密钥</param>
        /// <param name="keySize">密钥位数（128位，256位）</param>
        /// <returns>解密失败返回 null</returns>
        public static string Decrypt(string decrypt, string key, int keySize) {
            try {
                byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(key);
                byte[] strDecryptArray = Convert.FromBase64String(decrypt);
                byte[] resultArray = null;
                using (RijndaelManaged rDel = new RijndaelManaged()) {
                    rDel.KeySize = keySize;
                    rDel.Key = keyArray;
                    rDel.Mode = CipherMode.ECB;
                    rDel.Padding = PaddingMode.Zeros;
                    ICryptoTransform cTransform = rDel.CreateDecryptor();
                    resultArray = cTransform.TransformFinalBlock(strDecryptArray, 0, strDecryptArray.Length);
                }
                return UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch {
                return null;
            }
        }

  
    }
}
